<!-- 添加司机页面的司机删除服务enterdriver_delete.php  -->
<?php
// 包含连接数据库的文件
include('connection.php');

// 调用 Connect() 函数来连接数据库
$conn = Connect();


if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET['driver_id'])) {
    $driver_id = $_GET['driver_id'];

    // 查询司机是否已被使用
    $check_query = "SELECT driver_availability FROM driver WHERE driver_id = $driver_id";
    $check_result = mysqli_query($conn, $check_query);

    if ($check_result) {
        $row = mysqli_fetch_assoc($check_result);
        $availability = $row['driver_availability'];

        // 只有在可用性为 'yes' 的情况下才能删除司机信息
        if ($availability === 'yes') {
            // 执行删除操作
            $delete_query = "DELETE FROM driver WHERE driver_id = $driver_id";
            $delete_result = mysqli_query($conn, $delete_query);

            if ($delete_result) {
                // 删除成功，返回到 enterdriver.php 页面
                header("Location: enterdriver.php");
                exit();
            } else {
                echo "删除失败";
            }
        } else {
            echo "无法删除，司机正在被使用";
        }
    } else {
        echo "查询失败";
    }
} else {
    echo "无效请求";
}
